﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNHK.BLL;

namespace QLNHK
{
    public partial class frmCauHinhTienIch : Form
    {
        public frmCauHinhTienIch()
        {
            InitializeComponent();
        }

        private void frmCauHinhTienIch_Load(object sender, EventArgs e)
        {
            this.cboThanhPho.ValueChanged -= new System.EventHandler(this.cboThanhPho_ValueChanged);
            this.cboQuanHuyen.ValueChanged -= new System.EventHandler(this.cboQuanHuyen_ValueChanged);
            Global.setDataToComboThanhPho(cboThanhPho);
            cboThanhPho.Value = ConfigNHK.Install.ThanhPho;

            int cboThanhPhoValue = 0;
            Int32.TryParse(cboThanhPho.Value.ToString(), out cboThanhPhoValue);
            if (cboThanhPhoValue > 0)
                Global.setDataToComboQuanHuyen(cboQuanHuyen, Convert.ToInt32(cboThanhPho.Value));
            cboQuanHuyen.Value = ConfigNHK.Install.QuanHuyen;

            int cboQuanHuyenValue = 0;
            Int32.TryParse(cboQuanHuyen.Value.ToString(), out cboQuanHuyenValue);
            if (cboQuanHuyenValue > 0)
                Global.setDataToComboXaPhuong(cboXaPhuong, Convert.ToInt32(cboQuanHuyen.Value));

            string capsudung = "QUANHUYEN";
            if(ConfigNHK.Install.CapSuDung !=null)
                capsudung = ConfigNHK.Install.CapSuDung;
            cboCapSuDung.Value = capsudung;

            this.cboThanhPho.ValueChanged += new System.EventHandler(this.cboThanhPho_ValueChanged);
            this.cboQuanHuyen.ValueChanged += new System.EventHandler(this.cboQuanHuyen_ValueChanged);

        }

        private void cboThanhPho_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPho.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPho.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyen, Convert.ToInt32(cboThanhPho.Value));
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                int _ThanhPho = 0;
                int _QuanHuyen = 0;
                int _XaPhuong = 0;

                if (cboThanhPho.Value != null)
                    _ThanhPho = Convert.ToInt32(cboThanhPho.Value);

                if (cboQuanHuyen.Value != null)
                    _QuanHuyen = Convert.ToInt32(cboQuanHuyen.Value);

                if (cboXaPhuong.Value != null)
                    _XaPhuong = Convert.ToInt32(cboXaPhuong.Value);

                ConfigNHK.Install.ThanhPho = _ThanhPho;
                ConfigNHK.Install.QuanHuyen = _QuanHuyen;
                ConfigNHK.Install.XaPhuong = _XaPhuong;
                ConfigNHK.Install.CapSuDung = cboCapSuDung.Value.ToString();
                ConfigNHK.SaveConfig(ConfigNHK.Install);
                frmWait frmWait = new QLNHK.frmWait();
                Program.formMain.builtTreeDiaBan(ref frmWait);
                this.Cursor = Cursors.Default;
                MessageBox.Show("Lưu cấu hình thành công!.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show("Có lỗi phát sinh:"+ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboQuanHuyen_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyen.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyen.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuong, Convert.ToInt32(cboQuanHuyen.Value));
            }
        }
    }
}
